skynet = require "skynet"
cluster = require "skynet.cluster"
serpent = require "serpent"
crypt = require "skynet.crypt"
xxtea = require "xxtea"
cjson = require "cjson"
protoc = require "protoc"	 
pb = require "pb"
errno = require "errno"

urllib = require "http.url"
sockethelper = require "http.sockethelper"
socket = require "skynet.socket"
wsnetpack = require "websocketnetpack"
httpd = require "http.httpd"
httpc = require "http.httpc"

crypt = require "skynet.crypt"
codec = require 'codec'

Randkey = "vdNwFdfvgsd3dsdfv9xgdt34wed3sdyD"
math.randomseed(tostring(os.time()):reverse():sub(1, 7))

log = require "log" 
parseConfig = require "parse_config"


--打印数据的二进制
function PrintBin(s)
    s=string.gsub(s,"(.)",function (x) return string.format("%02X ",string.byte(x)) end)
    return s
end

--得到系统命令行输出
--解析主机号[cmd]= nslookup sdk2.entinfo.cn
function GetExcuteCmd(cmd)
    local t = io.popen(cmd)
    local ret = t:read("*all")
    return ret
end

--加载服务下所有pb文件
function LoadFiles(self)
    --游戏节点加载本游戏的pb
    if self.node < 1000 then 
        --log.info("g_packType[xx]=", g_packType[self.node])
        for packId, file in pairs(g_packType[self.node]) do
            self[file] = require (file)
            --self[file]:Init(packId, self)
            PbLoad(g_node_conf[self.node].proto, file)
        end
    else
        self["game_common"] = require ("game_common")
        PbLoad(g_node_conf[self.node].proto, "game_common")
        PbLoad("./game/ermj/", "ermj")
    end
end

--是否游客
function IsGuest(id)
    id = tostring(id)
    if string.sub(id, 1, 1) == '8' then
        return true
    end
end

--将对象转为mysql中识别的字符串,df为默认值(看个人习惯调用该组装函数)
--call pr_get_channel_cfg('%s')这种就不用加
--call pr_get_channel_cfg(%s), 这种就要加
function Obj2mstr(obj, df)
    if not obj then
        if df then
            return df
        end
        return 'NULL'
    end
    if type(obj) == 'string' then
        return "'"..obj.."'"
    else
        return obj
    end
end

--tableCopy
function TableCopy(src)
    assert(src)

    local des = {}
    for k, v in pairs(src) do
        des[k] = v
    end
    return des
end

--xxtea加密
function XteaEncode(content)
    local encrypt_data = xxtea.encrypt(content, Randkey)
    local ret = crypt.base64encode(encrypt_data)
    return ret
end

--xxtea解密
function XteaDecode(content)
    local encrypt_data = crypt.base64decode(content)
    local ret = xxtea.decrypt(encrypt_data, Randkey)
    return ret
end

--保留n位小数,up为true是保留至向上,否则是抹去
function KeepDecimal(s, n, up)      
    n = n or 2
    if n < 0 then
        return s
    end
    local is_str = false
    if type(s) == 'string' then
        s = tonumber(s)
        is_str = true
    end
    local inte = math.tointeger(s)
    if inte then           --已经是整数
        return inte
    end
    if s then
        local x = s % 1
        if x > 0 then      --小数部分
            local xd = string.sub(x, 1, 2 + n)
            local ret = math.floor(s) + tonumber(xd)
            if up then
                if math.abs(s - ret) > 1e-5 then    --必须这么比较
                    ret = ret + 0.1^n
                end
            end
            s = ret
        end
    end
    if is_str then
        s = tostring(s) or 0
    end
    return s or 0
end

--管理节点处理消息分发 
function ManagerDispatchMsg(manager, fd, msgData)
    local packId, msg = PbDecode(manager.node, msgData)
    if not packId then 
        return
    end
     
    local f = manager[g_packType[manager.node][packId]]
    if f then
        for cmd, req in pairs(msg) do
            if f[cmd] then 
                log.info("ManagerDispatchMsg cmd=", cmd)
                return f[cmd](fd, req)
            else
                log.error("ManagerDispatchMsg no cmd: ", packId.."-->"..cmd)
            end
        end
    else
        log.error("ManagerDispatchMsg no func! node:", manager.node.."-->packId: "..packId)
    end
   
end

function gen_nickname()
    local rd_list = {'0','1','2','3','4','5','6','7','8','9',
          'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
          'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',}
    local rd1 = math.random(1, 62)
    local rd2 = math.random(1, 62)
    local rd3 = math.random(1, 62)
    local rstr = rd_list[rd1]..rd_list[rd2]..rd_list[rd3]
    return 'yk'..rstr
end

function gen_headimgurl()
    local sex = g_sex.Male
    if math.random(1, 100) > 70 then
        sex = g_sex.Female
    end
    local base_hid = math.random(1, 14)
    local headimgurl = base_hid
    if sex == g_sex.Female then
        headimgurl = 200 + headimgurl
    end
    return headimgurl, sex
end

